﻿using System.Collections.Generic;
using UnityEngine;
using YunHao;

namespace UnknowWorld.CardGame
{

    public class CharacterCardControl : MonoBehaviour
    {
        /// <summary>
        /// 手牌位置
        /// </summary>
        public HandCardContainer cardContainer;
        /// <summary>
        /// 卡牌数
        /// </summary>
        public int CardCount
        {
            get { return _cards.Count; }
        }

        /// <summary>
        /// 抽卡时调用
        /// </summary>
        public System.Action<Card[]> WhenDrawCard { get; set; }

        /// <summary>
        /// <summary>
        /// 本局数据
        /// </summary>
        private Character _character;
        /// <summary>
        /// 本局卡牌
        /// </summary>
        private Queue<Card> _cards;

        /// <summary>
        /// 初始化卡牌数据
        /// </summary>
        public void InitData(Character character)
        {
            _character = character;
            InitCard();
        }

        /// <summary>
        /// 清空卡牌
        /// </summary>
        public void Clear()
        {
            cardContainer.ClearCard();
        }

        /// <summary>
        /// 初始化卡牌
        /// </summary>
        void InitCard()
        {
            //洗牌
            _cards = new Queue<Card>();
            Card[] cards = Common.DisorderArray(_character.Cards.ToArray());
            foreach (Card card in cards)
            {
                _cards.Enqueue(card);
            }
        }

        /// <summary>
        /// 补满手牌
        /// </summary>
        public void FullHandCard()
        {
            int count = _character.HandCardCount - cardContainer.CardCount;
            DrawSomeCard(count);
        }

        /// <summary>
        /// 获取下几张卡
        /// </summary>
        /// <param name="count"></param>
        Card[] NextCards(int count)
        {
            List<Card> cards = new List<Card>();
            while (count > 0)
            {
                if (_cards.Count > 0)
                {
                    cards.Add(_cards.Dequeue());
                }
                count--;
            }
            return cards.ToArray();
        }

        /// <summary>
        /// 抽一定数量的卡到手牌
        /// </summary>
        /// <param name="count"></param>
        public void DrawSomeCard(int count)
        {
            Card[] cards = NextCards(count);
            cardContainer.CreateCards(cards);
            if (null != WhenDrawCard) WhenDrawCard(cards);
        }
    }
}